import { BASE_URL } from './const';
// 网络请求封装
interface RequestOptions {
  url: string;
  data?: any;
  header?: any;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  timeout?: number;
  dataType?: string;
  responseType?: string;
}

// 请求基础URL，根据环境设置不同的baseURL

// 默认请求头
const DEFAULT_HEADER = {
  'content-type': 'application/json',
};

// 请求拦截器
const requestInterceptor = (options: RequestOptions): RequestOptions => {
  // 可以在这里统一添加token等操作
  // 例如: options.header.token = uni.getStorageSync('token');
  return options;
};

// 响应拦截器
const responseInterceptor = (response: any, resolve: Function, reject: Function) => {
  if (response.statusCode >= 200 && response.statusCode < 300) {
    // 请求成功
    resolve(response.data);
  } else {
    // 处理错误状态码
    handleError(response);
    reject(response);
  }
};

// 错误处理
const handleError = (response: any) => {
  // 根据状态码处理不同错误
  switch (response.statusCode) {
    case 401:
      // 未授权，可能需要登录
      uni.showToast({
        title: '请先登录',
        icon: 'none'
      });
      // 可以在这里执行跳转登录页等操作
      break;
    case 403:
      uni.showToast({
        title: '没有权限',
        icon: 'none'
      });
      break;
    case 404:
      uni.showToast({
        title: '请求资源不存在',
        icon: 'none'
      });
      break;
    case 500:
    case 502:
      uni.showToast({
        title: '服务器错误',
        icon: 'none'
      });
      break;
    default:
      uni.showToast({
        title: response.data?.message || '请求失败',
        icon: 'none'
      });
  }
};

// 基础请求方法
const request = (options: RequestOptions) => {
  // 合并请求头
  const header = Object.assign({}, DEFAULT_HEADER, options.header || {});
  
  // 完整URL
  const url = /^(http|https):\/\//.test(options.url) 
    ? options.url 
    : BASE_URL + options.url;
  
  // 请求参数
  const requestOptions = {
    url,
    data: options.data,
    header,
    method: options.method || 'GET',
    timeout: options.timeout || 60000,
    dataType: options.dataType || 'json',
    responseType: options.responseType || 'text'
  };
  
  // 应用请求拦截器
  const finalOptions = requestInterceptor(requestOptions);
  
  // 返回Promise
  return new Promise((resolve, reject) => {
    uni.request({
      ...finalOptions,
      success: (res) => {
        responseInterceptor(res, resolve, reject);
      },
      fail: (err) => {
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        });
        reject(err);
      }
    });
  });
};

// 导出请求方法
const http = {
  // GET请求
  get: (url: string, data?: any, header?: any) => {
    return request({
      url,
      method: 'GET',
      data,
      header
    });
  },
  
  // POST请求
  post: (url: string, data?: any, header?: any) => {
    return request({
      url,
      method: 'POST',
      data,
      header
    });
  },
  
  // PUT请求
  put: (url: string, data?: any, header?: any) => {
    return request({
      url,
      method: 'PUT',
      data,
      header
    });
  },
  
  // DELETE请求
  delete: (url: string, data?: any, header?: any) => {
    return request({
      url,
      method: 'DELETE',
      data,
      header
    });
  },
  
  // 自定义请求
  request
};

export default http; 